package com.taobao.dao.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.eclipse.swt.graphics.Image;

import com.taobao.dao.CommonDao;
import com.taobao.dao.UserInfoLevelImageDao;
import com.taobao.pojo.Config;
import com.taobao.pojo.HttpContext;

public class UserInfoLevelImageDaoImpl extends CommonDao implements UserInfoLevelImageDao {

    @Override
    public Object process(HttpContext context) throws Exception {
        InputStream is = context.getEntity().getContent();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int flag = 0;
        byte[] buffer = new byte[1024 * 5];
        while ((flag = is.read(buffer)) != -1) {
            baos.write(buffer, 0, flag);
        }
        
        File file = new File(Config.java_path + "/image/degree.gif");
        if(!file.exists()){
            file.createNewFile();
        }
        OutputStream o = new FileOutputStream(file);
        o.write(baos.toByteArray());
        o.flush();
        o.close();
        
        Image image = new Image(null, Config.java_path + "/image/degree.gif");
        return image;
    }

    @Override
    public Image getLevelImage(String imageUrl) throws Exception {
        return (Image) this.getRequestForStream(imageUrl);
    }
}
